CH9 turtle 海龜繪圖模組


使用迴圈搭配 Python 內建的 turtle 海龜模組(module)繪圖。

module(模組)是一組事先定義好的功能集合。它可以讓我們在需要使用時引用,讓你不用重複重造輪子(自己自幹重新撰寫),就可以達到你想要使用的功能


使用 turtle 模組方法 forward 直行(相對於 forward 為 backward 朝相反方向):

# import 引入 turtle 模組
import turtle

# 使用 turtle 模組方法,讓海龜向前直線爬行長度 100 px(整數非字串)
turtle.forward(100)

可以手動創建 turtle 物件或是自動生成,若是需要控制多個物件則可以使用t = turtle.Turtle()p = turtle.Pen() 來產生物件進行操作。若只需要一個畫筆則直接引用 turtle 後使用即可。

# import 引入 turtle 模組
import turtle

#color(畫筆, 填滿顏色),參數為 畫筆顏色, 填滿顏色
turtle.color('red','yellow')

#開始填充
turtle.begin_fill()

while True:
  #往畫筆指向向前移動
  turtle.forward(200)
  #角度逆時針170度(right為順時針)
  turtle.left(170)
  #turtle.pos() 會回傳目前筆頭 (x, y) 座標 tuple,若 x 和 y 座標同時絕對值小於 0 時停止(由官方文件的 abs(pos()) 改寫,避免 repl.it 環境無法執行)
  if turtle.pos()[0]<1 and turtle.pos()[1]<1:
    break

# 結束填滿
turtle.end_fill()
# 結束繪圖
turtle.done()

練習: turtle 海龜繪圖模組初體驗


引入 turtle 模組並繪製一條沿著 x 軸向右 200px 的直線。

#引入turtle模組
import turtle

turtle.color('blue')

#讓海龜向前直線爬行長度 200(整數非字串)
turtle.forward(200)

#需要結束關閉
turtle.done()

使用 turtle 製作簡易圖形


turtle 的世界的(x, y) 座標軸會長這樣,也就是說我們一開始 forward 會是沿著 x 軸向右(初始為 0, 0):

來源
透過turtle.left()turtle.right()表示逆時針和順時針。

#引入turtle模組
import turtle

#把繪筆指標reset回到原點
turtle.reset()

turtle.forward(100)
turtle.backward(50)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(50)

turtle.done()

使用 turtle reset


使用 turtle.reset() 可以讓我們繪筆回到原點:

# 引入 turtle 模組
import turtle

# 讓海龜向前直線爬行長度 50(整數非字串)
turtle.forward(50)

# 把指標 reset 回到原點
turtle.reset()

turtle.done()

使用 turtle 製作簡易圖形 II


使用 turtle.reset() 回到原點,turtle.clear() 清除畫布內容:

# 引入 turtle 模組
import turtle

# 讓海龜向前直線爬行長度 50(整數非字串)
turtle.forward(50)

# 把指標 reset 回到原點,同時清除畫布
turtle.reset()

# 僅清除畫布
turtle.clear()

turtle.done()

使用 turtle 填滿顏色


#引入turtle模組
import turtle

#把繪筆指標reset回到原點
turtle.reset()
#填滿顏色
turtle.fillcolor('red')
#開始填滿
turtle.begin_fill()

turtle.forward(100)
turtle.backward(50)
turtle.right(90)
turtle.forward(100)

turtle.right(90)
turtle.forward(20)
turtle.right(90)
turtle.forward(50)


#結束填滿
turtle.end_fill()

turtle.done()

練習


#引入turtle模組
import turtle

turtle.reset()
turtle.fillcolor('blue')
turtle.begin_fill()
turtle.forward(50)
turtle.right(90)
turtle.forward(10)
turtle.right(90)
turtle.forward(15)
turtle.left(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(35)
turtle.right(90)
turtle.forward(15)
turtle.right(90)
turtle.forward(10)
turtle.right(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(10)
turtle.left(90)
turtle.forward(45)
turtle.left(90)
turtle.forward(30)
turtle.right(90)
turtle.forward(10)
turtle.end_fill()
turtle.done()

移動畫筆


移動畫筆(有點像是畫水彩移動繪筆的感覺),turtle.up()改變狀態,將畫筆拿起,然後可以透過 turtle.goto(x, y)傳入要位移到的 x, y 座標點,最後 turtle.down()將筆放下:

引入turtle模組
import turtle

turtle.reset()

x =100
y =200
turtle.forward(100)
turtle.up()
# 移動海龜畫筆到座標 x, y 處
turtle.goto(x, y)
# 將筆放下
turtle.down()

# 讓海龜向前直線爬行長度 100
turtle.forward(100)

turtle.done()

使用 turtle 製作進階圖形


透過 while 迴圈可以慢慢畫出個圓形:times 變數(要命名為 counter 或其他名稱都可以)只是用來當作一個計數器,因為要轉 360 度,所以當每轉一度時 times 變數增加一。當累積到 360 時就會跳出 while 迴圈(因為不符合 while times < 360 條件) ,最後結果會是一個圓形。

import turtle

#計算圓形角度數
count = 0

#在角度<360度下,執行迴圈
while count < 360:

#每次向前1,增加角度1
    turtle.forward(1)
    turtle.right(1)
#每次迴圈,角度累加1度
    count += 1
turtle.done()

測驗:繪製多邊形(for)


設計一個程式可以依照使用者輸入畫出對應邊數量的多邊形(假設邊總長度為 400,外角和為 360)。

import turtle

#讓使用者輸入想要的邊數
input_sides = int(input('please keyin how many sides you want: '))

#讓使用者選擇多邊形顏色
input_color = input('please keyin what color you want: ')

#每一邊角度符合外角和加總為360度
angle = 360.0 / input_sides
#每一邊長度符合總長度加總為400
length = 400.0 / input_sides

#以side這個迭代變數遍歷「使用者輸入的邊數」
for side in range(input_sides):
    turtle.color(input_color)
#每次前進400/使用者輸入的邊數
    turtle.forward(length)
#每次順時針旋轉360/使用者輸入的邊數
    turtle.right(angle)

turtle.done()






你可能感興趣的文章

2017,讓我們再來看看 Web Components 吧!

2017,讓我們再來看看 Web Components 吧!

拜託,請你愛用 console.log

拜託,請你愛用 console.log

Git 與 GitHub

Git 與 GitHub






留言討論